﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bacteria
{
    public partial class frmBacteria : Form
    {
        public frmBacteria()
        {
            InitializeComponent();
        }

        const double startingSizeLower = 0;
        const double startingSizeUpper = 100;
        const double harvestLevelLower = 0;
        const double harvestLevelUpper = 50;
        const double harvestSizeLower = 0;
        const double maxSizeUpper = 1000;
        const double maxDaysLower = 0;
        const double maxDaysUpper = 366;
        const double growthRateLower = 0;
        const double growthRateUpper = 500;

        double startingSize, harvestLevel, harvestSize, maxSize, growthRate;
        int maxDays;
        string outputData, message;
        
        private void getVars()
        {
            startingSize = double.Parse(txtStartingSize.Text);                  // Starting size of Sample
            harvestLevel = double.Parse(txtHarvestLevel.Text);                  // Size harvest available
            harvestSize = double.Parse(txtHarvestSize.Text);                    // Size of harvest
            maxSize = double.Parse(txtMaxSize.Text);                            // Maximum size tolerated
            maxDays = int.Parse(txtMaxDays.Text);                               // Maximum shelf life
            growthRate = double.Parse(txtGrowthRate.Text) / 100 + 1;            // Size increase per day
        }

        bool validStartingSize(double testStartingSize)
        {
            if (testStartingSize > startingSizeLower && testStartingSize < startingSizeUpper)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool validHarvestLevel(double testHarvestLevel)
        {
            if (testHarvestLevel > harvestLevelLower && testHarvestLevel < harvestLevelUpper)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool validHarvestSize(double testHarvestSize, double harvestLevel)
        {
            if (testHarvestSize > harvestSizeLower && testHarvestSize < harvestLevel)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool validMaxSize(double startingSize, double testMaxSize)
        {
            if (testMaxSize > startingSize && testMaxSize < maxSizeUpper)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        string simulate(double startingSize, double harvestLevel, double harvestSize, double maxSize, int maxDays, double growthRate)
        {
            double cultureMass = startingSize;      // Current Mass
            int dayCounter = 0;                     // Counts days of culturing
            bool sizeBlown = false;                 // Break loop if size blown

            string Output = "\n";

            // After MAXDAYS days, culture becomes unreliable
            // If size also exceeds MAXSIZE, culture also becomes unreliable

            while (dayCounter < maxDays && sizeBlown == false)
            {
                dayCounter += 1;
                cultureMass *= growthRate;

                // Inform of size of sample
                Output += "Day " + dayCounter + " - current sample size is " + cultureMass + "g.\r\n";

                if (cultureMass > maxSize)
                {
                    sizeBlown = true;
                    Output += "\r\nCulture has passed usable size - Dispose";
                }
                else
                {
                    // culture able to be harvested when harvest level reached
                    if (cultureMass >= harvestLevel)
                    {
                        // process harvest
                        cultureMass -= harvestSize;                        
                        Output += "Harvesting " + harvestSize + "g, leaving " + cultureMass + "g.\r\n";
                    }
                    else
                    {
                        if (dayCounter >= maxDays)
                        {
                            Output += "\r\nCulture has exceeded maximum age - Dispose";
                        }
                        else
                        {
                            // inform of no harvest
                            Output += "No Harvest.\r\n";
                        }
                    }
                }
                outputData = Output;
            }
            return outputData;
        }

        private void btnSimulate_Click(object sender, EventArgs e)
        {
            getVars();

            if (!validStartingSize(startingSize))
            {
                message = "Starting Size is invalid.\r\nPlease enter a value greater than " + startingSizeLower + " and less than " + startingSizeUpper + ".";
                txtOutput.Text = message;
                return;
            }

            if (!validHarvestLevel(harvestLevel))
            {
                message = "The size when harvest available is invalid.\r\nPlease enter a value greater than " + harvestLevelLower + " and less than " + harvestLevelUpper + ".";
                txtOutput.Text = message;
                return;
            }

            if (!validHarvestSize(harvestSize, harvestLevel))
            {
                message = "The size of harvest is invalid.\r\nPlease enter a value greater than " + harvestSizeLower + " and less than " + harvestLevel + ".";
                txtOutput.Text = message;
                return;
            }

            if (!validMaxSize(startingSize, maxSize))
            {
                message = "The max size entered is invalid.\r\nPlease enter a value greater than " + startingSize + " and less than " + maxSize + ".";
                txtOutput.Text = message;
                return;
            }
            
 
           
            simulate(startingSize, harvestLevel, harvestSize, maxSize, maxDays, growthRate);
            txtOutput.Text = outputData;
        }
    }
}
